﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengoo.DB
{
    public class DBFactory
    {
        /// <summary>
        /// 连接的数据库类型，OLEDB或者SQL
        /// </summary>
        public static string ConnectionType
        {
            get
            {
                return GetAppSettings("connType");
            }
        }

        /// <summary>
        /// 数据库的连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                string conn = GetAppSettings("conn");
                if (ConnectionType == "oledb")
                {
                    return GetServerMapPath(conn);
                }
                return conn;
            }
        }
        /// <summary>
        /// 创建IDBHelper，根据数据类型，默认创建SQL的DBHelper
        /// </summary>
        /// <param name="connectionType">oledb或sql</param>
        /// <returns></returns>
        public static IDBHelper CreateDBHelper(string connectionType)
        {
            IDBHelper dbhelper = null;
            switch (connectionType)
            {
                case "oledb":
                    dbhelper = new DBHelperOledb(ConnectionString);
                    break;
                default:
                    dbhelper = new DBHelperSql(ConnectionString);
                    break;
            }
            return dbhelper;
        }

        /// <summary>
        /// 创建IDBHelper
        /// </summary>
        /// <returns></returns>
        public static IDBHelper CreateDBHelper()
        {
            return CreateDBHelper(ConnectionType);
        }

        public static string GetServerMapPath(string path)
        {
            return System.Web.HttpContext.Current.Server.MapPath(path);
        }

        /// <summary>
        /// 获取WebConfig的配置项值
        /// </summary>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string GetAppSettings(string flag)
        {
            return System.Configuration.ConfigurationSettings.AppSettings[flag];
        }
    }
}
